function Phase_out = BPS_up(Fre_out,Constell)
% 盲相位搜索 2024.6.24 By YMZ
Constell = 2;
Phase_in=Fre_out;
Phase_in = sqrt((2^Constell-1)/1.5)*Phase_in./sqrt(mean(abs(Phase_in).^2,2));
B =2^(Constell+1); % 这么多个区间
N=25;
Phase_mat = (pi/2)*(-B/2:B/2-1)/B;
Phase_mat = reshape(Phase_mat,[],1);%%测试相位取值
QAM_symbol=qammod(0:2^Constell-1,2^Constell);
AA=exp(-1j*Phase_mat);
AA=repmat(AA,1,length(Phase_in));%%B*N的数组，每一行表示每一种测试相位的序列
Phase_out_mat = AA.*repmat(Phase_in,B,1); %%每一组测试相位调制后的结果
Phase_out_mat_1=reshape(Phase_out_mat,1,[]);
QAM_symbol=repmat(QAM_symbol.',1,length(Phase_out_mat_1));
AA=abs(Phase_out_mat_1-QAM_symbol);
[~,pos1]=min(AA);
Phase_out_mat_A=QAM_symbol(pos1);
Phase_out_mat_A=reshape(Phase_out_mat_A,B,[]);%%每个经过测试相位旋转后的理想取值

nn=1;
pos_list = zeros(1,length(2*N+1:length(Phase_in)-(2*N+1)));
i = 1;
for num_fg = 2*N+1:length(Phase_in)-(2*N+1)
    Phase_out_mat_A1=Phase_out_mat_A(:,num_fg-2*N:num_fg+2*N);
    Phase_out_mat1=Phase_out_mat(:,num_fg-2*N:num_fg+2*N);
    error = abs(Phase_out_mat_A1-Phase_out_mat1);
    [~,pos]=min(mean(error,2));
    pos_list(i) = pos;
    i = i+1;
    POS(nn)=pos;
    if nn>2 && (POS(nn)-POS(nn-1))<-2^(Constell)
        Phase_mat=Phase_mat+pi/2;
    end
    if nn>2 && (POS(nn)-POS(nn-1))>2^(Constell)
        Phase_mat=Phase_mat-pi/2;
    end
    nn=nn+1;
    Phase_out(num_fg) = Phase_in(num_fg)*exp(-1j*Phase_mat(pos));
end
figure;plot(POS)
Phase_out=Phase_out-mean(Phase_out);
scatterplot(Phase_out);
% scatterplot(Phase_out);
% figure;plot(Phase_out,'.');
%%
% xita_list = zeros(1,length(2*N+1:length(Phase_in)-(2*N+1)-2*N));
% Phase_out2 = xita_list;
% i = 1;
% for num_fg = 2*N+1:length(Phase_in)-(2*N+1)-2*N
%     xk = Fre_out(:,num_fg-2*N:num_fg+2*N);
%     yk = Phase_out(:,num_fg-2*N:num_fg+2*N);
%     hn = sum(xk.*conj(yk));
%     xita = angle(exp(hn));
%     xita_list(i) = xita;
%     i = i+1;
%     Phase_out2(num_fg-2*N) = Phase_out(num_fg).*exp(1i*(-xita));
% end
% figure;
% plot(xita_list);
% figure;
% scatterplot(Phase_out2);


